package org.eclipse.soc.ide4edu.assignment.editors;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.StringTokenizer;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

public class BufferStore {

	IFile file = null;
	String buffer = null;

	final String TextMarker = "--TEXT--";
	final String StyleMarker = "--STYLE--";

	public BufferStore() {
	}

	public BufferStore(IFile in) {
		this.file = in;
	}

	public String getBuffer() {

		if (file != null) {
			String t;
			String buff = new String();
			try {
				InputStream stream = file.getContents();
				DataInputStream reader = new DataInputStream(stream);
				while ((t = reader.readLine()) != null)
					buff += t + "\n";

			} catch (CoreException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return buff;
		} else
			return buffer;
	}

	public IFile getFile() {
		return file;
	}

	public StyleRange[] getStyleRanges(Display display) {

		int R = 0, G = 0, B = 0;
		int length = 0, i = 0;
		String stylesData = getStylesData();
		StringTokenizer info = new StringTokenizer(stylesData);

		String lenToken = info.nextToken();

		if (lenToken.compareTo("null") == 0)
			return null;
		else
			length = Integer.parseInt(lenToken);

		StyleRange[] finalRanges = new StyleRange[length];

		try {

			while (info.hasMoreTokens()) {

				int start = Integer.parseInt(info.nextToken());
				int len = Integer.parseInt(info.nextToken());

				int fontStyle = 0;

				String font = info.nextToken();
				if (font.compareTo("null") != 0) {
					if (Integer.parseInt(font) == 1 << 0)
						fontStyle = SWT.BOLD;
					if (Integer.parseInt(font) == 1 << 1)
						fontStyle = SWT.ITALIC;
				}

				StyleRange style = new StyleRange(start, len, null, null,
						fontStyle);

				if (info.nextToken().compareTo("true") == 0)
					style.underline = true;

				if (info.nextToken().compareTo("true") == 0)
					style.strikeout = true;

				R = Integer.parseInt(info.nextToken());
				G = Integer.parseInt(info.nextToken());
				B = Integer.parseInt(info.nextToken());

				RGB rgb = new RGB(R, G, B);
				Color foreColor = new Color(display, rgb);
				style.foreground = foreColor;

				R = Integer.parseInt(info.nextToken());
				G = Integer.parseInt(info.nextToken());
				B = Integer.parseInt(info.nextToken());

				rgb = new RGB(R, G, B);
				Color backColor = new Color(display, rgb);

				style.background = backColor;
				finalRanges[i++] = style;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return finalRanges;
	}

	public String getStylesData() {
		String buffer = getBuffer();
		String[] text = buffer.split(StyleMarker);

		if (text != null) {
			text[1] = text[1].trim();
			return text[1];
		}
		return null;
	}

	public String getText() {
		String buffer = getBuffer();
		String[] text = buffer.split(TextMarker);

		if (text != null) {
			text[1] = text[1].trim();
			return text[1];
		}
		return null;
	}

	public void initEdit(StyledText edit, Display display) {

		try {

			StyleRange[] styles = getStyleRanges(display);
			String text = getText();
			edit.setText(text);
			if (styles != null)
				edit.setStyleRanges(styles);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void setBuffer(String buff) throws CoreException {

		this.buffer = new String(buff);

		if (file != null)
			file.setContents(new ByteArrayInputStream(buffer.getBytes()), 0,
					null);
	}

}
